package com.bj.zspace.cloud.order.listener;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 1.应用启动监听
 * 2.监听nacos配置变化拿到变化后的值
 * 3.发送通知
 */
@Slf4j
@Service
public class NacosConfigListener implements ApplicationRunner {

    @Resource
    private NacosConfigManager nacosConfigManager;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        ConfigService configService = nacosConfigManager.getConfigService();

        configService.addListener("service-order.properties", "order", new Listener() {
            @Override
            public Executor getExecutor() {
                return Executors.newFixedThreadPool(2);
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                log.info("收到配置更新:" + configInfo);
            }
        });

    }

}
